<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      @submit.native.prevent
    >
      <!-- <el-form-item label="用户类型" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择用户类型" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="联系人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
         <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['men:menNeedInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['men:menNeedInfo:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['men:menNeedInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['men:menNeedInfo:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="menNeedInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="需求id" align="center" prop="id" /> -->
      <el-table-column label="类型" align="center" prop="typeId" />
      <!-- <el-table-column label="发布人" align="center" prop="userId" /> -->
      <!-- <el-table-column label="用户类型" align="center" prop="status" :formatter="statusFormat" /> -->
      <el-table-column
        label="需求标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="描述"
        align="center"
        prop="intro"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="是否上架"
        align="center"
        prop="stype"
        :formatter="stypeFormat"
      />
      <el-table-column
        label="领取人数"
        align="center"
        prop="num"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="公司名字"
        align="center"
        prop="company"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="所属地区"
        align="center"
        prop="city"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系人"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系电话"
        align="center"
        prop="phone"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="默认/推荐"
        align="center"
        prop="recommend"
        :formatter="recommendFormat"
      />
      <el-table-column label="创建时间" align="center" prop="caAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.caAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="upAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['men:menNeedInfo:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleNeedUser(scope.row)"
            v-hasPermi="['men:menNeedUser:list']"
            >领取详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleRecommend(scope.row)"
            v-hasPermi="['men:menNeedInfo:edit']"
            >推荐</el-button
          >
          <el-button
            v-if="scope.row.stype==1"
            size="mini"
            type="text"
            icon="el-icon-star-on"
            @click="handlePut(scope.row)"
            v-hasPermi="['men:menNeedInfo:edit']"
            >上架</el-button
          >
          <el-button
            v-if="scope.row.stype==0"
            size="mini"
            type="text"
            icon="el-icon-star-off"
            @click="handleUnder(scope.row)"
            v-hasPermi="['men:menNeedInfo:edit']"
            >下架</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['men:menNeedInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="发布人" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择发布人">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="需求标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入需求标题" />
        </el-form-item>
        <el-form-item label="描述" prop="intro">
          <el-input
            v-model="form.intro"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="公司名字" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名字" />
        </el-form-item>
        <el-form-item label="所属地区" prop="city">
          <el-input v-model="form.city" placeholder="请输入所属地区" />
        </el-form-item>
        <el-form-item label="联系人" prop="name">
          <el-input v-model="form.name" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="0默认1推荐" prop="recommend">
          <el-input v-model="form.recommend" placeholder="请输入0默认1推荐" />
        </el-form-item>
        <el-form-item label="创建时间" prop="caAt">
          <el-date-picker
            clearable
            size="small"
            v-model="form.caAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="upAt">
          <el-date-picker
            clearable
            size="small"
            v-model="form.upAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="0默认1是删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入0默认1是删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title"
      :visible.sync="recommend"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="默认/推荐" prop="recommend" label-width="100">
          <el-radio-group v-model="form.recommend">
            <el-radio
              v-for="dict in recommendOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
          <!-- <el-input v-model="form.recommend" placeholder="请输入默认/推荐" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 领取详情 -->
    <el-dialog
      :title="title"
      :visible.sync="needUser"
      width="900px"
      append-to-body
    >
      <el-table
        v-loading="loading"
        :data="menNeedUserList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="领取人" align="center" prop="name" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="领取时间" align="center" prop="caAt">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.caAt, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMenNeedInfo,
  getMenNeedInfo,
  getMenNeedUser,
  underMenNeedInfo,
  putMenNeedInfo,
  delMenNeedInfo,
  addMenNeedInfo,
  updateMenNeedInfo,
  exportMenNeedInfo,
} from "@/api/men/menNeedInfo";

export default {
  name: "MenNeedInfo",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求详情表格数据
      menNeedInfoList: [],
      // 用户领取需求表格数据
      menNeedUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //推荐弹出层
      recommend: false,
      //领取详情
      needUser: false,
      // 用户类型字典
      statusOptions: [],
      // 0默认1推荐字典
      recommendOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeId: [
          { required: true, message: "类型不能为空", trigger: "change" },
        ],
        userId: [
          { required: true, message: "发布人不能为空", trigger: "change" },
        ],
        status: [
          { required: true, message: "用户类型不能为空", trigger: "blur" },
        ],
        title: [
          { required: true, message: "需求标题不能为空", trigger: "blur" },
        ],
        intro: [{ required: true, message: "描述不能为空", trigger: "blur" }],
        city: [
          { required: true, message: "所属地区不能为空", trigger: "blur" },
        ],
        name: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
        ],
        recommend: [
          { required: true, message: "0默认1推荐不能为空", trigger: "blur" },
        ],
        caAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        delFlag: [
          { required: true, message: "0默认1是删除不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    // this.getDicts("men_user_type").then(response => {
    //   this.statusOptions = response.data;
    // });
    this.getDicts("men_recommend_status").then((response) => {
      this.recommendOptions = response.data;
      console.log(response);
    });
  },
  methods: {
    /** 查询需求详情列表 */
    getList() {
      this.loading = true;
      listMenNeedInfo(this.queryParams).then((response) => {
        this.menNeedInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用户类型字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 0默认1推荐字典翻译
    recommendFormat(row, column) {
      return this.selectDictLabel(this.recommendOptions, row.recommend);
    },
    // 0已上架1已下架
    stypeFormat(row, column) {
      if (row.stype == 0) {
        return "已上架";
      }

      if (row.stype == 1) {
        return "已下架";
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.recommend = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeId: null,
        userId: null,
        status: 0,
        title: null,
        intro: null,
        company: null,
        city: null,
        name: null,
        phone: null,
        recommend: null,
        caAt: null,
        upAt: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加需求详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMenNeedInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改需求详情";
      });
    },
    /** 领取详情 */
    handleNeedUser(row) {
      this.reset();
      const id = row.id || this.ids;
      getMenNeedUser(id).then((response) => {
        this.menNeedUserList = response.rows;
        this.needUser = true;
        this.title = "领取详情";
        this.loading = false;
      });
    },
    /** 推荐按钮操作 */
    handleRecommend(row) {
      this.reset();
      const id = row.id || this.ids;
      getMenNeedInfo(id).then((response) => {
        this.form = response.data;
        this.recommend = true;
        this.title = "设置推荐";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMenNeedInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.recommend = false;
              this.getList();
            });
          } else {
            addMenNeedInfo(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.recommend = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 上架 */
    handlePut(row) {
      if (row.stype == 0) {
        return alert("该需求已上架");
      }
      const ids = row.id || this.ids;
      this.$confirm("是否上架需求", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(function () {
          return putMenNeedInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        });
    },
    /** 下架 */
    handleUnder(row) {
      if (row.stype == 1) {
        return alert("该需求已下架");
      }
      const ids = row.id || this.ids;
      this.$confirm("是否下架需求", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(function () {
          return underMenNeedInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除需求详情选中数据", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delMenNeedInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有需求详情数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportMenNeedInfo(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
